package com.uestc.simchat;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.hyphenate.EMCallBack;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import java.util.List;

public class SCChatActivity extends AppCompatActivity implements EMMessageListener {

    private TextView contentView;
    private EditText inputView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        contentView = (TextView) findViewById(R.id.sc_text_content);
        inputView = (EditText) findViewById(R.id.sc_edit_message);

        final String chatId=getIntent().getStringExtra("username");

        findViewById(R.id.sc_btn_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputView.getText().toString();
                EMMessage message = EMMessage.createTxtSendMessage(content, chatId);
                message.setChatType(EMMessage.ChatType.Chat);

                EMClient.getInstance().chatManager().sendMessage(message);
                contentView.setText(contentView.getText() + "\n" + content);
                message.setMessageStatusCallback(new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        Log.i("SC", "消息发送成功: " + chatId);
                    }

                    @Override
                    public void onError(int i, String s) {
                        Log.e("SC", "消息发送失败 "+ i + ", " + s);
                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
            }
        });
    }

    @Override
    public void onMessageReceived(List<EMMessage> list) {
        for (final EMMessage message: list) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    contentView.setText(contentView.getText() + "\n" + ((EMTextMessageBody) message.getBody()).getMessage());
                }
            });
        }
    }

    @Override
    public void onCmdMessageReceived(List<EMMessage> list) {

    }

    @Override
    public void onMessageRead(List<EMMessage> list) {

    }

    @Override
    public void onMessageDelivered(List<EMMessage> list) {

    }

    @Override
    public void onMessageRecalled(List<EMMessage> list) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        EMClient.getInstance().chatManager().addMessageListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EMClient.getInstance().chatManager().removeMessageListener(this);
    }
}
